import {reactive} from './03_reactive.js'

// 判断是否为对象 ，注意排除 null
const isObject = val => val !== null && typeof val === 'object';

// 转换value: 如果val是对象, 生成对应的proxy对象
const convert = val => isObject(val) ? reactive(val) : val

class RefImpl {
  constructor (value) {
    this._rawValue = value
    this._value = convert(value)
  }

  get value () {
    console.log('监视到读value, 收集依赖')
    return this._value
  } 
  
  set value (newValue) {
    if (this._rawValue!==newValue) {
      this._rawValue = newValue
      this._value = convert(newValue)
      console.log('监视到写value, 派发更新')
    }
  }
}

export function ref(value) {
  return new RefImpl(value)
}
